package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Category;
import com.draven.mall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品分类控制器
 */
@RestController
@RequestMapping("/api/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类（平铺结构）
     */
    @GetMapping("/list")
    public ApiResponse getCategoryList() {
        List<Category> categories = categoryService.getAllCategories();
        return ApiResponse.success(categories);
    }

    /**
     * 获取分类树结构
     */
    @GetMapping("/tree")
    public ApiResponse getCategoryTree() {
        List<Category> categoryTree = categoryService.getCategoryTree();
        return ApiResponse.success(categoryTree);
    }

    /**
     * 获取分类详情
     */
    @GetMapping("/{id}")
    public ApiResponse getCategoryDetail(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        if (category != null) {
            return ApiResponse.success(category);
        } else {
            return ApiResponse.error("分类不存在");
        }
    }

    /**
     * 添加分类
     */
    @PostMapping("/add")
    public ApiResponse addCategory(@RequestBody Category category) {
        boolean success = categoryService.addCategory(category);
        if (success) {
            return ApiResponse.success("添加分类成功");
        } else {
            return ApiResponse.error("添加分类失败");
        }
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public ApiResponse updateCategory(@RequestBody Category category) {
        boolean success = categoryService.updateCategory(category);
        if (success) {
            return ApiResponse.success("更新分类成功");
        } else {
            return ApiResponse.error("更新分类失败");
        }
    }

    /**
     * 删除分类
     */
    @PostMapping("/delete")
    public ApiResponse deleteCategory(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        if (id == null) {
            return ApiResponse.error("参数错误");
        }
        
        boolean success = categoryService.deleteCategory(id);
        if (success) {
            return ApiResponse.success("删除分类成功");
        } else {
            return ApiResponse.error("删除分类失败");
        }
    }

    /**
     * 更新分类状态
     */
    @PostMapping("/status")
    public ApiResponse updateCategoryStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        Integer status = Integer.parseInt(params.get("status").toString());
        
        boolean success = categoryService.updateCategoryStatus(id, status);
        if (success) {
            return ApiResponse.success("更新状态成功");
        } else {
            return ApiResponse.error("更新状态失败");
        }
    }
} 